-- LeiTingDataReport
-- Created by chengb Oct/8/2016
-- 负责雷霆平台数据上报

module("LeiTingDataReport", package.seeall);

-- 秘钥
local KEY = "#Leiting#";

-- 游戏标识
local GAME_CODE;
if DEBUG_MODE == 1 then
    GAME_CODE = "gumballsTest";
else
    GAME_CODE = "gumballs";
end

-- 游戏类别(1:手游,2:端游,3:页游)
local GAME_TYPE = 1;

-- 激活上报地址
local ACTIVE_REPORT_URL = "http://logmonitor.leiting.com/api/mg_log!addMgActivateLog.action";

-- 充值上报地址
local CHARGE_REPORT_URL = "http://logmonitor.leiting.com/api/mg_log!addMgChargeLog.action";

-- 创建角色上报地址
local CREATE_CHARGE_REPORT_URL = "http://logmonitor.leiting.com/api/mg_log!addMgCreateRoleLog.action";

-- 登录登出上报地址
local LOGIN_REPORT_URL = "http://logmonitor.leiting.com/api/mg_log!addMgLoginLog.action";

-- 注册账号上报地址
local REGISTER_ACCOUNT_REPORT_URL = "http://logmonitor.leiting.com/api/mg_log!addMgRegisterLog.action";

-- 检测激活上报（激活定义为玩家安装完毕在联网状态下首次启动）
function checkActiveReport()
    if (not NetworkStatus.isWifiConnected() and
        not NetworkStatus.isMobileConnected()) then
        -- 当前尚未联网
        return;
    end

    if hasReportedActive() then
        -- 已经上报过了
        return;
    end

    -- 上报
    reportActive();
end

-- 是否进行过激活上报
function hasReportedActive()
    return UserDefaultM.getStringForKey("lt_active_report") == "1";
end

-- 激活上报
function reportActive()
    local args = {};

    -- 调用日志接口时的当前时间，格式为：yyyy-MM-dd HH:mm:ss;
    local createDate = os.date("%Y-%m-%d %H:%M:%S", TimeM.getServerTime());

    local gameCode   = GAME_CODE;
    local gameType   = GAME_TYPE;
    local key        = KEY;
    local mac        = NetworkStatus.getMacAddress();
    local imei       = getIMEI();
    local osVer      = getOSVersion();
    local terminInfo = getMobileModel();
    local channel    = LeiTingAgent.getChannelNo();
    local media      = LeiTingAgent.getMedia();    -- 媒体分包编码

    -- 签名,md5(gameCode+key+createDate),key找接口提供方提供
    local sign = string.lower(cc.MD5:getMD5(gameCode..key..createDate));

    args.gameType   = gameType;
    args.gameCode   = gameCode;
    args.createDate = createDate;
    args.sign       = sign;
    args.mac        = mac;
    args.imei       = imei;
    args.osVer      = osVer;
    args.terminInfo = terminInfo;
    args.channel    = channel;
    args.media      = media;

    local jsonParams = json.encode(args);

    trace("LeiTingDataReport", "reportActive : jsonParams = " .. jsonParams);

    local data = "params=" .. jsonParams;
    local function callback(response)
        trace("LeiTingDataReport", "reportActive response = %o", response);
        result = string.parseJsonToTable(response);
        if result.status == "success" then
            -- 标记上报成功
            UserDefaultM.setStringForKey("lt_active_report", "1");
        end
    end
    postDataToRemoteHost(ACTIVE_REPORT_URL, data, callback);
end

--[[ 如下功能已搬到服务器！！！

-- 创建角色上报
-- @param args table类型的参数
function reportCreateChar(args)
    assert(type(args) == "table", "args必须为table类型");

    local accountName = args.accountName;
    assert(type(accountName) == "string", "必须传入accountName");

    -- 调用日志接口时的当前时间，格式为：yyyy-MM-dd HH:mm:ss;
    local createDate = os.date("%Y-%m-%d %H:%M:%S", TimeM.getServerTime());

    local gameCode    = GAME_CODE;
    local gameType    = GAME_TYPE;
    local key         = KEY;
    local ip          = NetworkStatus.getIPAddress();
    local mac         = NetworkStatus.getMacAddress();
    local imei        = getIMEI();
    local osVer       = getOSVersion();
    local terminInfo  = getMobileModel();

    -- 签名,md5(accountName+key+createDate),key找接口提供方提供
    local sign = string.lower(cc.MD5:getMD5(accountName..key..createDate));

    args.gameType   = gameType;
    args.gameCode   = gameCode;
    args.createDate = createDate;
    args.sign       = sign;
    args.ip         = ip;
    args.mac        = mac;
    args.imei       = imei;
    args.osVer      = osVer;
    args.terminInfo = terminInfo;

    local jsonParams = json.encode(args);
    trace("LeiTingDataReport", "reportCreateChar : jsonParams = " .. jsonParams);

    local data = "params=" .. jsonParams;
    local function callback(response)
        trace("LeiTingDataReport", "reportCreateChar response = %o", response);
    end
    postDataToRemoteHost(CREATE_CHARGE_REPORT_URL, data, callback);
end

-- 登录登出上报
-- @param args table类型的参数
-- 使用样例：LeiTingDataReport.reportLogin({accountName="chengb", loginMode=1,loginType=1,newAccount=1,creditAccount=1})
function reportLogin(args)
    assert(type(args) == "table", "args必须为table类型");

    local accountName = args.accountName;
    assert(type(accountName) == "string", "必须传入accountName");

    -- 调用日志接口时的当前时间，格式为：yyyy-MM-dd HH:mm:ss;
    local createDate = os.date("%Y-%m-%d %H:%M:%S", TimeM.getServerTime());

    -- 上次登录时间
    local lastLoginDate = os.date("%Y-%m-%d %H:%M:%S", ME.user.dbase:query("last_login_time"));

    local gameCode    = GAME_CODE;
    local gameType    = GAME_TYPE;
    local key         = KEY;
    local ip          = NetworkStatus.getIPAddress();
    local mac         = NetworkStatus.getMacAddress();
    local imei        = getIMEI();
    local osVer       = getOSVersion();
    local terminInfo  = getMobileModel();

    -- 签名,md5(accountName+key+createDate),key找接口提供方提供
    local sign = string.lower(cc.MD5:getMD5(accountName..key..createDate));

    args.gameType      = gameType;
    args.gameCode      = gameCode;
    args.createDate    = createDate;
    args.lastLoginDate = lastLoginDate;
    args.sign          = sign;
    args.ip            = ip;
    args.mac           = mac;
    args.imei          = imei;
    args.osVer         = osVer;
    args.terminInfo    = terminInfo;

    -- 样例：
    -- jsonParams ='{"mac":"","terminInfo":"","creditAccount":1,"loginMode":1,"accountName":"chengb","gameType":1,"ip":"","osVer":"","loginType":1,"createDate":"2016-10-09 20:23:18","imei":"","sign":"879394c3a9c5d2ef54c588a33ae5326b","gameCode":"gumballsTest","lastLoginDate":"2016-10-09 20:18:39","newAccount":1}'
    local jsonParams = json.encode(args);

    trace("LeiTingDataReport", "reportLogin : jsonParams = " .. jsonParams);

    local data = "params=" .. jsonParams;

    local function callback(response)
        trace("LeiTingDataReport", "reportLogin response = %o", response);
    end
    postDataToRemoteHost(LOGIN_REPORT_URL, data, callback);
end

-- 注册账号上报
-- @param args table类型的参数
function reportRegisterAccount(args)
    assert(type(args) == "table", "args必须为table类型");

    local accountName = args.accountName;
    assert(type(accountName) == "string", "必须传入accountName");

    -- 调用日志接口时的当前时间，格式为：yyyy-MM-dd HH:mm:ss;
    local createDate = os.date("%Y-%m-%d %H:%M:%S", TimeM.getServerTime());

    local gameCode    = GAME_CODE;
    local gameType    = GAME_TYPE;
    local key         = KEY;
    local ip          = NetworkStatus.getIPAddress();
    local mac         = NetworkStatus.getMacAddress();
    local imei        = getIMEI();
    local osVer       = getOSVersion();
    local terminInfo  = getMobileModel();

    -- 签名,md5(accountName+key+createDate),key找接口提供方提供
    local sign = string.lower(cc.MD5:getMD5(accountName..key..createDate));

    args.gameType   = gameType;
    args.gameCode   = gameCode;
    args.createDate = createDate;
    args.sign       = sign;
    args.ip         = ip;
    args.mac        = mac;
    args.imei       = imei;
    args.osVer      = osVer;
    args.terminInfo = terminInfo;

    local jsonParams = json.encode(args);
    trace("LeiTingDataReport", "reportRegisterAccount : jsonParams = " .. jsonParams);

    local data = "params=" .. jsonParams;
    local function callback(response)
        trace("LeiTingDataReport", "reportRegisterAccount response = %o", response);
    end
    postDataToRemoteHost(REGISTER_ACCOUNT_REPORT_URL, data, callback);
end

-- 充值上报
-- @param args table类型的参数
function reportCharge(args)
    assert(type(args) == "table", "args必须为table类型");

    local accountName = args.accountName;
    assert(type(accountName) == "string", "必须传入accountName");

    -- 调用日志接口时的当前时间，格式为：yyyy-MM-dd HH:mm:ss;
    local createDate = os.date("%Y-%m-%d %H:%M:%S", TimeM.getServerTime());

    local gameCode    = GAME_CODE;
    local gameType    = GAME_TYPE;
    local key         = KEY;
    local ip          = NetworkStatus.getIPAddress();
    local mac         = NetworkStatus.getMacAddress();
    local imei        = getIMEI();
    local osVer       = getOSVersion();
    local terminInfo  = getMobileModel();

    -- 签名,md5(accountName+key+createDate),key找接口提供方提供
    local sign = string.lower(cc.MD5:getMD5(accountName..key..createDate));

    args.gameType   = gameType;
    args.gameCode   = gameCode;
    args.createDate = createDate;
    args.sign       = sign;
    args.ip         = ip;
    args.mac        = mac;
    args.imei       = imei;
    args.osVer      = osVer;
    args.terminInfo = terminInfo;

    local jsonParams = json.encode(args);

    trace("LeiTingDataReport", "reportCharge : jsonParams = " .. jsonParams);

    local data = "params=" .. jsonParams;
    local function callback(response)
        trace("LeiTingDataReport", "reportCharge response = %o", response);
    end
    postDataToRemoteHost(CHARGE_REPORT_URL, data, callback);
end

--]]
